package com.xiaochen.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochen.manager.service.SysUserService;
import com.xiaochen.model.dto.system.AssginRoleDto;
import com.xiaochen.model.dto.system.SysUserDto;
import com.xiaochen.model.entity.system.SysUser;
import com.xiaochen.model.vo.common.Result;
import com.xiaochen.model.vo.common.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

//    /**
//     * 1.分页查询
//     */
//    @GetMapping("/getSysUserByPage/{pageNum}/{pageSize}")
//    public Result findByPage(SysUserDto sysUserDto,
//                             @PathVariable Integer pageNum,
//                             @PathVariable Integer pageSize) {
//        PageInfo<SysUser> sysUserList = sysUserService.findByPage(sysUserDto,pageNum,pageSize);
//        return Result.build(sysUserList, ResultCodeEnum.SUCCESS);
//    }

    /**
     * 1.分页查询-----方案2
     */
    @GetMapping("/getSysUserByPage")
    public Result findByPage(SysUserDto sysUserDto,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize) {
        PageInfo<SysUser> sysUserList = sysUserService.findByPage(sysUserDto,pageNum,pageSize);
        return Result.build(sysUserList, ResultCodeEnum.SUCCESS);
    }

    /**
     * 2.新增用户
     */
    @PostMapping("addSysUser")
    public Result addSysUser(@RequestBody SysUser sysUser) {
        sysUserService.addSysUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 3.修改用户
     */
    @PutMapping("/updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser) {
        sysUserService.updateSysUser(sysUser);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/deleteById/{userId}")
    public Result deleteById(@PathVariable Integer userId) {
        sysUserService.deleteById(userId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据ID查询用户信息
     */
    @GetMapping("/selectById/{userId}")
    public Result selectById(@PathVariable Integer userId){
        List<SysUser> sysUserInfo = sysUserService.selectById(userId);
        return Result.build(sysUserInfo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 为用户分配角色,并保存到数据库
     */
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleDto assginRoleDto){
        sysUserService.doAssign(assginRoleDto);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}
